package com.boot.notes.event;

import com.boot.modules.admin.entity.SysUserEntity;
import com.boot.modules.admin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * 发布事件   实际业务中，在spring管理的类中 比如：控制层或者实现层，直接注入发布即可
 *
 * @Description:
 * @Author: mkq
 * @Date: 2021/10/8 16:49
 **/
//@Component
@Service
public class PushEvent {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 用户注册
     */
    public void registerUser(SysUserEntity user) {
        sysUserService.saveUser(user);
        applicationContext.publishEvent(new RegisterEvent("注册", user.getMobile(), user.getUserId()));
//        applicationEventPublisher.publishEvent(new RegisterEvent("注册", user.getMobile(), user.getUserId()));
//        applicationEventPublisher.publishEvent(new OrderEvent("订单", user.getMobile(), user.getUserId()));
    }

    public void pushEvent() {

        applicationContext.publishEvent(new RegisterEvent("注册", "1384383838438", 2l));
//        applicationEventPublisher.publishEvent(new RegisterEvent("注册", "1384383838438", 2l));
//        applicationEventPublisher.publishEvent(new OrderEvent("订单", "1384383838438", 2l));
    }
}
